<?php
// +----------------------------------------------------------------------
// | Longbing [ WE CAN DO IT JUST THINK IT ]
// +----------------------------------------------------------------------
// | Copyright Chengdu longbing Technology Co., Ltd.
// +----------------------------------------------------------------------
// | Website http://longbing.org/
// +----------------------------------------------------------------------
// | Sales manager: +86-13558882532 / +86-13330887474
// | Technical support: +86-15680635005
// | After-sale service: +86-17361005938
// +----------------------------------------------------------------------
namespace app\website\controller;

use app\AdminRest;
use app\website\model\CardVideoCategory as CategoryModel;
use think\App;

class AdminVideoCategory extends AdminRest{
    protected $page;
    protected $list_rows;

    // 继承 验证用户登陆
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->page = !empty($this->_param['page']) ? $this->_param['page'] : 1;
        $this->list_rows = !empty($this->_param['limit']) ? $this->_param['limit'] : 10;

        $this->card_video_category = new CategoryModel();
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/14 14:31
     * @功能说明: 获取分类列表
     */
    public function lists(){
        $where[] = ['uniacid','=',$this->_uniacid];
        if(!isset($this->_param['modular_id'])){
            return $this->error('缺少模块id');
        }
        $where[] = ['modular_id','=',$this->_param['modular_id']];
        $list = $this->card_video_category->lists($where,$this->page, $this->list_rows);
        return $this->success($list);
    }

    public function listAll(){
        $where[] = ['uniacid','=',$this->_uniacid];
        if(!isset($this->_param['modular_id'])){
            return $this->error('缺少模块id');
        }
        $where[] = ['modular_id','=',$this->_param['modular_id']];
        $list = $this->card_video_category->lists($where); #查询所有的分类
        return $this->success($list);
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/14 14:33
     * @功能说明: 添加分类
     */
    public function addCategory(){
        $param = $this->_param;
        $name = isset($param['name'])?trim($param['name']):false;
        $param['top']    = isset($param['top'])?$param['top']:1; #排序默认等于1 不得小于0
        $param['status'] = isset($param['status'])?$param['status']:1; #默认显示
        $modular_id      = $param['modular_id']; //模块id
        if(!$modular_id){
            return $this->error('所属模块不能为空');
        }
        if(!$name){
            return $this->error('分类名称不能为空');
        }
        if($param['top'] < 0){
            return $this->error(' 排序不能小于0');
        }
        $param['uniacid'] = $this->_uniacid;
        $this->card_video_category->createRow($param);
        return $this->success([]);
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/14 14:31
     * @功能说明:分类删除
     */
    public function delete(){
        $id = $this->_param['id'];
        $count = $this->card_video_category->where(['id'=>$id,'uniacid'=>$this->_uniacid])->find();
        if(!$count){
            return $this->error('数据不存在');
        }
        if($count['video_count'] > 0){
            return $this->error('分类下有视频文件不可删除');
        }
        if($this->card_video_category->destroyRow(['id'=>$id])){
            return $this->success([]);
        }
        return $this->error('删除失败');
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/15 14:23
     * @功能说明:单独修改分类展示状态
     */
    public function updateStatus(){
        $param = $this->_param;
        if(!$param['id']){
            return $this->error('请选择你要修改的分类');
        }
        if($this->card_video_category->update(['status'=>$param['status']],['id'=>$param['id']])){
            return $this->success([]);
        }
        return $this->error('修改失败');
    }

    /**
     **@author lichuanming
     * @DataTime: 2020/5/14 14:32
     * @功能说明: 分类修改
     */
    public function update(){
        $param = $this->_param;
        $name = isset($param['name'])?$param['name']:false;
        $top = isset($param['top'])?$param['top']:1; #排序默认等于1 不得小于0
        $status = isset($param['status'])?$param['status']:1; #默认显示
        if(!$name){
            return $this->error('分类名称不能为空');
        }
        if($top < 0){
            return $this->error(' 排序不能小于0');
        }
        if(!$param['id']){
            return $this->error('请选择你要修改的分类');
        }
        if($this->card_video_category->update(['top'=>$top,'name'=>$name,'status'=>$status],['id'=>$param['id']])){
            return $this->success([]);
        }
        return $this->error('删除失败');
    }
}